﻿using jlynet.core.Library;
using jlynet.core.notebook.model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace jlynet.core.notebook.util
{
    public class INIFileInitialization
    {
        public static string INIPath = Library.SysHelper.GetStartupPath() + "//jlynet-notebook.ini";

        /// <summary>
        /// 初始化程序配置文件
        /// </summary>
        public void initialization()
        {
            INIModel model = new INIModel();
            model.node = "Options";
            List<keyValue> keyValues = new List<keyValue>();

            keyValue keyValue01 = new keyValue();
            keyValue01.name = "ShowNotice";
            keyValue01.value = "False";
            keyValues.Add(keyValue01);

            keyValue keyValue02 = new keyValue();
            keyValue02.name = "LastFile";
            keyValue02.value = SQLiteConfigHelper.DatabaseFile;
            keyValues.Add(keyValue02);

            keyValue keyValue03 = new keyValue();
            keyValue03.name = "PromptMinToTray";
            keyValue03.value = "False";
            keyValues.Add(keyValue03);

            keyValue keyValue04 = new keyValue();
            keyValue04.name = "WinLeft";
            keyValue04.value = 363;
            keyValues.Add(keyValue04);

            keyValue keyValue05 = new keyValue();
            keyValue05.name = "WinTop";
            keyValue05.value = 149;
            keyValues.Add(keyValue05);

            keyValue keyValue06 = new keyValue();
            keyValue06.name = "WinRight";
            keyValue06.value = 1003;
            keyValues.Add(keyValue06);

            keyValue keyValue07 = new keyValue();
            keyValue07.name = "WinBottom";
            keyValue07.value = 619;
            keyValues.Add(keyValue07);

            keyValue keyValue08 = new keyValue();
            keyValue08.name = "ContentWidth";
            keyValue08.value = 160;
            keyValues.Add(keyValue08);

            model.keyValue = keyValues;
            initialization(model);
        }

        /// <summary>
        /// 写入程序配置文件
        /// </summary>
        /// <param name="model"></param>
        public void initialization(INIModel model)
        {
            INIHelper ini = new INIHelper(INIPath);
            for (int i = 0; i < model.keyValue.Count; i++)
            {
                string name = model.keyValue[i].name;
                object value = model.keyValue[i].value;
                string str = value.ToString();
                ini.IniWriteValue(model.node, name, str);
            }
        }

        /// <summary>
        /// 写入程序配置文件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="INIPath_str"></param>
        public void initialization(INIModel model,string INIPath_str)
        {
            INIHelper ini = new INIHelper(INIPath_str);
            for (int i=0;i< model.keyValue.Count;i++)
            {
                string name = model.keyValue[i].name;
                object value = model.keyValue[i].value;
                string str = value.ToString();
                ini.IniWriteValue(model.node, name, str);
            }
        }

        /// <summary>
        /// 读取程序配置文件中的数据库路径
        /// </summary>
        /// <returns></returns>
        public string getDatabaseFilePath()
        {
            if (File.Exists(INIFileInitialization.INIPath))
            {
                INIHelper ini = new INIHelper(INIPath);
                return ini.IniReadValue("Options", "LastFile");
            }else
            {
                return "";
            }   
        }

        /// <summary>
        /// 读取程序配置文件中的数据库路径
        /// </summary>
        /// <param name="INIPath_str"></param>
        /// <returns></returns>
        public string getDatabaseFilePath(string INIPath_str)
        {
            if (File.Exists(INIPath_str))
            {
                INIHelper ini = new INIHelper(INIPath_str);
                return ini.IniReadValue("Options", "LastFile");
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 读取所有段落下的所有key的值
        /// </summary>
        /// <returns></returns>
        public List<string> IniReadValues()
        {
            if (File.Exists(INIFileInitialization.INIPath))
            {
                INIHelper ini = new INIHelper(INIPath);
                return ini.IniReadValues();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 读取指定段落下的所有key的值
        /// </summary>
        /// <param name="Section"></param>
        /// <returns></returns>
        public INIModel IniReadSectionValues(string Section)
        {
            if (File.Exists(INIFileInitialization.INIPath))
            {
                INIModel model = new INIModel();

                List<string> result = new List<string>();
                INIHelper ini = new INIHelper(INIPath);
                model.node = Section;
                List<keyValue> keyValues = new List<keyValue>();
                result = ini.IniReadSectionValues(Section);

                for (int i=0; i< result.Count; i++)
                {
                    string value = ini.IniReadValue(Section, result[i]);
                    keyValue keyValue = new keyValue();
                    keyValue.name = result[i];
                    keyValue.value = value;
                    keyValues.Add(keyValue);
                }

                model.keyValue = keyValues;
                return model;
            }else{
                return null;
            }
        }

        /// <summary>
        /// 读取指定段落下的所有key的值
        /// </summary>
        /// <param name="Section"></param>
        /// <returns></returns>
        public INIModel IniReadSectionValues2(string Section)
        {
            if (File.Exists(INIFileInitialization.INIPath))
            {
                INIModel model = new INIModel();
                Dictionary<string, Dictionary<string, object>> root = new Dictionary<string, Dictionary<string, object>>();
                Dictionary<string, object> node = new Dictionary<string, object>();

                INIHelper ini = new INIHelper(INIPath);
                model.node = Section;
                List<keyValue> keyValues = new List<keyValue>();
                root = ini.IniReadSectionValues2(Section);
                node = root[Section];

                foreach (KeyValuePair<string, object> kvp in node)
                {
                    keyValue keyValue = new keyValue();
                    keyValue.name = kvp.Key;
                    keyValue.value = kvp.Value;
                    keyValues.Add(keyValue);
                }
                model.keyValue = keyValues;
                return model;
            }else{
                return null;
            }
        }

        /// <summary>
        /// 读取指定段落下的指定key的值
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string getSectionKey(string sectionName, string key)
        {
            INIHelper ini = new INIHelper(INIPath);
            return ini.IniReadValue(sectionName, key);
        }

        /// <summary>
        /// 读取指定段落下的指定key的值
        /// </summary>
        /// <param name="INIPath_str"></param>
        /// <param name="sectionName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string getSectionKey(string INIPath_str, string sectionName, string key)
        {
            INIHelper ini = new INIHelper(INIPath_str);
            return ini.IniReadValue(sectionName, key);
        }

        /// <summary>
        /// 删除ini文件下某段落的某个key所有内容
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="key"></param>
        public void delSectionKey(string sectionName, string key)
        {
            INIHelper ini = new INIHelper(INIPath);
            ini.IniWriteValue(sectionName, key, null);
        }

        /// <summary>
        /// 删除ini文件下某段落的某个key所有内容
        /// </summary>
        /// <param name="INIPath_str"></param>
        /// <param name="sectionName"></param>
        /// <param name="key"></param>
        public void delSectionKey(string INIPath_str, string sectionName, string key)
        {
            INIHelper ini = new INIHelper(INIPath_str);
            ini.IniWriteValue(sectionName, key, null);
        }

        /// <summary>
        /// 删除ini文件下某段落的所有内容
        /// </summary>
        /// <param name="sectionName"></param>
        public void delSection(string sectionName)
        {
            INIHelper ini = new INIHelper(INIPath);
            ini.IniWriteValue(sectionName, null, null);
        }

        /// <summary>
        /// 删除ini文件下某段落的所有内容
        /// </summary>
        /// <param name="INIPath_str"></param>
        /// <param name="sectionName"></param>
        public void delSection(string INIPath_str, string sectionName)
        {
            INIHelper ini = new INIHelper(INIPath_str);
            ini.IniWriteValue(sectionName, null, null);
        }

        /// <summary>
        /// 删除ini文件下的所有内容
        /// </summary>
        public void del()
        {
            INIHelper ini = new INIHelper(INIPath);
            ini.IniWriteValue(null, null, null);
        }

        /// <summary>
        /// 删除ini文件下的所有内容
        /// </summary>
        /// <param name="INIPath_str"></param>
        public void del(string INIPath_str)
        {
            INIHelper ini = new INIHelper(INIPath_str);
            ini.IniWriteValue(null, null, null);
        }

    }
}
